<?php
/**
 * 房型类
 * @author jroam
 *
 */
class hotelroomAction extends Action{
	
	
	
	/**
	 * 显示这个客栈的所有房型,关编辑时间段价格
	 */
	function listroom(){
		$hid=$_REQUEST["hid"];//客栈ID
		$pid=$_REQUEST["pid"];//pid为价格的id值
		$MenuId=$_REQUEST["MenuId"];
		$PMID=$_REQUEST["PMID"];
		
	
		if(!is_numeric($hid)) die("传入参数不对");
		if($pid=="") $pid=0;
		if(!is_numeric($pid)) die("传入价格参数不对");
		$mr=M("hotelroom");
		$sql="select * from ".DQ."hotelroom where hotelroom2=$hid order by hotelroom18 desc";
		$rs=$mr->query($sql);
		
		//获取这个酒店的时间段设置值
		$sql="select * from ".DQ."price where price4=$hid and price3=612 order by price18 desc";
		$pricers=$mr->query($sql);
		
		//初始化值,当为0时，默认第一个
		$arr=array();
		if($pid>0){
			foreach ($pricers as $v){
				if($v["price0"]==$pid){
					$arr=$v;
				}
			}
		}
		
		
		$this->assign("pid",$pid);
		$this->assign("rs",$rs);
		$this->assign("hid",$hid);
		$this->assign("pricers",$pricers);
		$this->assign("initarr",$arr);
		$this->assign("MenuId",$MenuId);
		$this->display();
		
	}
	
	/**
	 * 修改酒店类型
	 */
	function edithotelprice(){
		$rid=$_REQUEST["rid"];
		$pid=$_REQUEST["pid"];//priceid
		
		if(!is_numeric($rid)) die("传入参数不正确");
		$mr=M("hotelroom");
		$sql="select * from ".DQ."hotelroom where hotelroom0=$rid order by hotelroom18 desc";
		$rs=$mr->query($sql);
		
		
		//获取时间段表内容
		$sql="select * from ".DQ."price where price4=".$rs[0]["hotelroom0"]." and price3=612";
		
		$rsprice=$mr->query($sql);
		
		//当pid不为空时，默认选择的值进行修改
		$thisv=array();
		if($pid!=""){
			foreach ($rsprice as $v){
				if($v["price0"]==$pid) $thisv=$v;
			}
		}
	
		$this->assign("rid",$rid);
		$this->assign("rs",$rs[0]);	
		$this->assign("rsprice",$rsprice);
		$this->assign("thisv",$thisv);
		$this->assign("pid",$pid);
		
		
		$this->display();
		
		require RootDir."/inc/Calendar.php";
	}
	
	
/**
 * 
 * 自动计算这个客栈的每个房型的最低价，以方便排序
 * @param $hid  客栈
 */
	function updateroomminprice(){
		
		
		//客栈id
		$hid=$_REQUEST["hid"];
		if(!is_numeric($hid)) return false;
		
		$mr=M("hotelroom");
		
		
		//获取他的所有时间段值,在这些时间段中选出最低价
		$sql="select * from ".DQ."price where price4=".$hid;
		$prs=$mr->query($sql);
		
		
		
		//获取这个酒店的所有房型
		$rrs=$mr->where("hotelroom2=".$hid)->select();
		
		
		
		$minv=9999999;//初始值
		
		$rarr=array();//存放最后计算的结果
		
		foreach($rrs as $v){
			if($prs){
			
				foreach ($prs as $v2){
					if(strlen($v2["price19"])>3){
						$temp="";
						$temp=getkuohaostr($v2["price19"],"/{id:".$v["hotelroom0"]."}[\{\}\w\.\:]*{mzlj:([\d\.]+)}/");
						
						if($temp<$minv and $temp!=""){
							if($rarr[$v["hotelroom0"]]){
								if($temp<$rarr[$v["hotelroom0"]]) $rarr[$v["hotelroom0"]]=$temp;
							}
							else{
								$rarr[$v["hotelroom0"]]=$temp;
							}
							
						}
						if(strlen($v["hotelroom12"])>4){
							//$temp2=getkuohaostr($v2["price19"],"/[\d]{4}\-[\d]{1,2}\-[\d]\|[\d\.]*\|([\d\.]+)/");
							
							preg_match("/[\d]{4}\-[\d]{1,2}\-[\d]{1,2}\|[\d\.]*\|[\d\.]+/",$v["hotelroom12"],$marr);
							
							foreach($marr as $d ){
								$temp2=getsubstr($d, "/[\d\.]+$/");
								
								if($temp2<$temp and $temp2!=""){
									$rarr[$v["hotelroom0"]]=$temp2;
									if($rarr[$v["hotelroom0"]]){
										if($temp2<$rarr[$v["hotelroom0"]]) $rarr[$v["hotelroom0"]]=$temp2;
									}
									else{
										$rarr[$v["hotelroom0"]]=$temp2;
									}
								}
							}
							
						}
					}
				}
			}
		}
		
		
	  $sqlsub="";
	  foreach($rarr as $k=> $v){
	  	$sqlsub="update ".DQ."hotelroom set   hotelroom7=".$v." where hotelroom0=".$k;
	  	
	  	$mr->execute($sqlsub);
	  }
	
	 // print_r($rarr);
	  
	  
	 return true;	  

	}
	/**
	 * 
	 */
	function updateurlpricemin(){
		
		$hid=$_REQUEST["hid"];//客栈id
		$flag=$this->updateroomminprice($hid);
		echo $flag?"更新成功":"更新失败";
	}
	
}

?>